<?php

    global $mikron;
    
    $uprating = new uprating();
    
    $mikron->Schema->Add(dirname(__FILE__).'/schema.xml', 'Схема модуля голосований');
    $mikron->OperatingInstructions->AddFunction('MIKRON_UPVOTING', 'OperatingInstruction_MikronUpVoting', $uprating);
    $mikron->OperatingInstructions->AddFunction('MIKRON_EX_FAVOURITES', 'OperatingInstruction_MikronShowFavourites', $uprating);
    $mikron->CSS->AddFile(dirname(__FILE__).'/style.css', 'Стиль модуля UpVoting');
    $mikron->Applications->Add($uprating);

    class uprating
    {
        function OperatingInstruction_MikronShowFavourites($parameters)
        {
            global $mikron;
            $cr = new Criteria();
            $cr->add(new Criterion('user', $mikron->Users->CurrentUser()->id()));
            $qr = $mikron->Queries->Query('MIKRON_FAVOURITE', null, $cr, '`entity` DESC');
            $last_entity_name = null;
            $table = null;
            while($fav = $qr->fetch())
            {
                $entity_name = $fav->entity->value;
                $entity_id = $fav->entityid->value;
                if($last_entity_name != $entity_name)
                {
                    if(!is_null($table))
                    {
                        $table->end();
                    }
                    $table = new html_table(1, null, '100%', 4);
                    $last_entity_name = $entity_name;
                    $caption = null;
                    switch($entity_name)
                    {
                        case 'PARTNER_PROGRAM':
                            $caption = 'Партнерские программы';
                            break;
                        case 'T_USER':
                            $caption = 'Пользователи';
                            break;
                    }
                    echo sprintf('<h1>%s</h1>', $caption);
                }
                if($entity_name == 'T_USER')
                {
                    $user = new $entity_name($entity_id);
                    $table->newcol(UserClass::ProfileLink($user, true, $user->login->value));
                }
                elseif($entity_name == 'PARTNER_PROGRAM')
                {
                    $prog = new $entity_name($entity_id);
                    $parameters = Array();
                    $parameters['programs'] = Array($prog);
                    $parameters['buttons'] = false;
                    $table->newcol();
                    $mikron->Site->draw_user_programs($parameters);
                }
            }
            if(!is_null($table))
            {
                $table->end();
            }
        }

        function up($parameters)
        {
            global $mikron;
            $for = $parameters['for'];
            $id = (int)$parameters['id'];
            if($this->is_rated($for, $id))
            {
                return;
            }
            $ur = new MIKRON_UPRATE();
            $ur->author->value = $mikron->Users->CurrentUser()->id();
            $ur->entity->value = $for;
            $ur->entityid->value = $id;
            $ur->Save();
        }
        function is_rated($for, $id)
        {
            global $mikron;
            $cr = new Criteria();
            $cr->add(new Criterion('author', $mikron->Users->CurrentUser()->id()));
            $cr->add(new Criterion('entity', $for));
            $cr->add(new Criterion('entityid', $id));
            $qr = $mikron->Queries->Query('MIKRON_UPRATE', null, $cr);
            return $qr->count() > 0;
        }
        function in_favourite_list($for, $id)
        {
            global $mikron;
            $cr = new Criteria();
            $cr->add(new Criterion('user', $mikron->Users->CurrentUser()->id()));
            $cr->add(new Criterion('entity', $for));
            $cr->add(new Criterion('entityid', $id));
            $qr = $mikron->Queries->Query('MIKRON_FAVOURITE', null, $cr);
            return $qr->count() > 0;
        }
        function to_favourite($parameters)
        {
            global $mikron;
            $for = $parameters['for'];
            $id = (int)$parameters['id'];
            if($this->in_favourite_list($for, $id))
            {
                return;
            }
            $object = new MIKRON_FAVOURITE();
            $object->user->value = $mikron->Users->CurrentUser()->id();
            $object->entity->value = $for;
            $object->entityid->value = $id;
            $object->Save();
        }
        
        function OperatingInstruction_MikronUpVoting($parameters)
        {
            global $mikron;
            $for = $parameters['entity'];
            $id = (int)$parameters['id'];
            $favourite = false;
            if(array_key_exists('favourite', $parameters))
            {
                $favourite = (int)$parameters['id'] == 1;
            }
            $app = $mikron->Applications->Item('uprating')->object;
            $upiconclass = array_key_exists('upiconclass', $parameters)?$parameters['upiconclass']:'plus1icon';
            $upiconclass_disabled = array_key_exists('upiconclass_disabled', $parameters)?$parameters['upiconclass_disabled']:'plus1icon_disabled';
            $favouriteicon_class = array_key_exists('favouriteclass', $parameters)?$parameters['favouriteclass']:'favouriteadd_icon_class';
            $favouriteicon_class_disabled = array_key_exists('favouriteclass_disabled', $parameters)?$parameters['favouriteclass_disabled']:'favouriteadd_icon_class_disabled';
            
            $caption = ' Хорошее!';
            if($app->is_rated($for, $id))
            {
                $mikron->Graphics->OperatingInstruction_IconLink(Array('iconclass'=>$upiconclass_disabled,'text'=>$caption,'title'=>$caption,'href'=>null));
                // echo sprintf('<span class="%s"></span><font color="aaaaaa">%s</font>', $upiconclass_disabled, $caption);
            }
            else
            {
                $mikron->Graphics->OperatingInstruction_IconLink(Array('iconclass'=>$upiconclass,'text'=>$caption,'title'=>$caption,'href'=>sprintf('?application=uprating&method=up&for=%s&id=%s', $for, $id)));
                // echo sprintf('<a href="?application=uprating&method=up&for=%s&id=%s"><span class="%s"></span>%s</a>', $for, $id, $upiconclass, $caption);
            }

            if($favourite)
            {
                if($app->in_favourite_list($for, $id))
                {
                    $caption = ' В избранном';
                    echo sprintf(' <span class="%s"></span><font color="aaaaaa">%s</font>', $favouriteicon_class_disabled, $caption);
                }
                else
                {
                    $caption = ' Добавить себе в избранное';
                    echo sprintf(' <a href="?application=uprating&method=to_favourite&for=%s&id=%s"><span class="%s"></span>%s</a>', $for, $id, $favouriteicon_class, $caption);
                }
            }
        }

    }

?>